package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang 杞﹁締鍏ュ綍淇濆瓨controller
 */
public class CarCreationResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarCreationResultController.class);
	
	private String carName;

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();

		// 娣诲姞浜嬪姟鎻愪氦寮傚父鎹曡幏
		try {
			controller.begin();
			Car car = Car.getInstance();
			Sequence seq = Sequence.getInstance();
			
			CarCreationInputViewHelper viewHelper = (CarCreationInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			viewHelper.setInsertFlag(1);
			car = setEntityFromViewHelper(viewHelper, car);
			TmsUserProfile profile = uc.getUserProfile();
			car.setUpdateUserId(profile.getUniqueId());
			car.setInsertUserId(profile.getUniqueId());
			uc.setAttribute("carlicense", viewHelper.getLicensePlate());//用于返回页查看
			uc.setAttribute("id", String.valueOf(viewHelper.getId()));
			
			// 浠巙c涓幏鍙栧浘鐗囦俊鎭�涔嬪悗娓呯┖
			// 涓婁紶鍥剧墖閮ㄥ垎锛屽垽鏂煇閮ㄤ綅鏄惁鏈夊浘鐗�鍥剧墖淇濆瓨鍦ㄥ浘鐗囪〃涓�

			List<CarImage> list = new ArrayList<CarImage>();

			if (viewHelper.getImg1() != null
					&& !"".equals(viewHelper.getImg1())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("1");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg1());
				list.add(entity);
			}
			if (viewHelper.getImg2() != null
					&& !"".equals(viewHelper.getImg2())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("2");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg2());
				list.add(entity);
			}
			if (viewHelper.getImg3() != null
					&& !"".equals(viewHelper.getImg3())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("3");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg3());
				list.add(entity);
			}
			if (viewHelper.getImg4() != null
					&& !"".equals(viewHelper.getImg4())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("4");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg4());
				list.add(entity);
			}
			if (viewHelper.getImg5() != null
					&& !"".equals(viewHelper.getImg5())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("5");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg5());
				list.add(entity);
			}
			if (viewHelper.getImg6() != null
					&& !"".equals(viewHelper.getImg6())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("6");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg6());
				list.add(entity);
			}
			if (viewHelper.getImg7() != null
					&& !"".equals(viewHelper.getImg7())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("7");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg7());
				list.add(entity);
			}
			if (viewHelper.getImg8() != null
					&& !"".equals(viewHelper.getImg8())) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("8");
				entity.setImgType("1");
				entity.setImgSrc(1);
				entity.setImgPath(viewHelper.getImg8());
				list.add(entity);
			}
				
				car.setInnerType(2);
				car.setId(viewHelper.getId());
			if(viewHelper.getEntryStatus() == 1){
				car.setUpBsflag(2);
			}
				
			String sysPath = (String) uc.getAttribute("sysPath");
			car = car.findCarByLicensePlate(car.getLicensePlate(),1);
			if(StringUtil.isNullOrBlank(car.getSerial())){
				String serial = seq.getSerial(1);
				String transferSerial = seq.getSerial(6);				
				car.setEntryStatus(4);//褰曞叆瀹屾垚				
				// 杞﹁締淇℃伅淇濆瓨
				car.insertCar(car, list, sysPath, serial,1);
				//库存车辆进场库记录信息插入
				//delete by huangheli at 20150817 start
//				MarketCarGateInfo marketCarGateInfo = MarketCarGateInfo.getInstance();
//				marketCarGateInfo.setSerial(serial);
//				marketCarGateInfo.setCarScore(viewHelper.getCarScore());
//				marketCarGateInfo.setInoutMark(1);//入库
//				marketCarGateInfo.insertGateInfo(marketCarGateInfo);
				//delete by huangheli at 20150817 end
				//存入过户信息表
				TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
				transferAcceptedInfo.setLicensePlate(viewHelper.getLicensePlate());
				transferAcceptedInfo.setVin(viewHelper.getVin());
				transferAcceptedInfo.setInsertUserid(profile.getUniqueId());
				transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());
				transferAcceptedInfo.setBasicCarId(serial);
				transferAcceptedInfo.setSerial(transferSerial);
				transferAcceptedInfo.setCarOwner(viewHelper.getCarOwner());
				transferAcceptedInfo.setCarOwnerIdCardNo(viewHelper.getCarOwnerIdCardNo());
				transferAcceptedInfo.setOwnerPhone(viewHelper.getOwnerPhone());
				transferAcceptedInfo.setOwnerType(Integer.parseInt(viewHelper.getOwnerType()));
				transferAcceptedInfo.setOwnerAddress(viewHelper.getOwnerAddress());
				transferAcceptedInfo.setOwnerOrgNo(viewHelper.getOwnerOrgNo());
				transferAcceptedInfo.setCy_cardType(viewHelper.getCardType());
				transferAcceptedInfo.insertEasyCar(transferAcceptedInfo);
				
				//过户流程操作记录表
				TransferProcessHistory transferProcessHistory = new TransferProcessHistory();
				transferProcessHistory.setSerial(transferSerial);
				transferProcessHistory.setBusinessType(0);
				transferProcessHistory.setInsertUserId(profile.getUniqueId());
				transferProcessHistory.insertHisInfoAtAssess(transferProcessHistory);
				
				//过户业务的财务收费信息表
				TransferFinanceInfo transferFinanceInfo = new TransferFinanceInfo();
				transferFinanceInfo.insertFinanceByInner(transferSerial);
				
			}else{
				//打出提示信息   该车辆已经存在数据库中！
				viewHelper.setInsertFlag(2);
				uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);			
			}
							
			
				
			uc.removeAttribute("imgList");
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 绯荤粺寮傚父
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}


	/**
	 * 鎶妚iewHelper鐨勫�璧嬬粰Entity
	 * 
	 * @param viewHelper
	 * @param car
	 * @return
	 * @throws TmsDaoException
	 */
	private Car setEntityFromViewHelper(CarCreationInputViewHelper viewhelper,
			Car car) throws TmsDaoException {
//		VehicleBrand brand = VehicleBrand.getInstance();
//		// 车辆名称取值
//		if (StringUtil.isNullOrBlank(viewhelper.getBrandId())) {
//			setCarName(" ");
//		} else {
//			List list = brand.getAllVehicleBrandInfo(viewhelper.getVendorId());
//			if (list.size() > 0) {
//				for (int i = 0; i < list.size(); i++) {
//					Map map = (Map) list.get(i);
//					if (map.get("brandId").equals(viewhelper.getBrandId())) {
//						String carName = (String) map.get("brandName");
//						setCarName(carName);
//					}
//				}
//			} else {
//				setCarName(" ");
//			}
//		}

		car.setCarName(viewhelper.getCarName());

		car.setAttribute(Integer.parseInt(viewhelper.getAttribute()));
		car.setBrandId(viewhelper.getBrandId());
		car.setCardType(Integer.parseInt(viewhelper.getCardType()));
		car.setLicensePlate(viewhelper.getLicensePlate());
		car.setCarOwner(viewhelper.getCarOwner());
		car.setCarOwnerIdCardNo(viewhelper.getCarOwnerIdCardNo());
		car.setCarType(Integer.parseInt(viewhelper.getCarType()));
		car.setCarUse(Integer.parseInt(viewhelper.getCarUse()));
		car.setColor(Integer.parseInt(viewhelper.getColor()));
		car.setCompanyContactPerson(viewhelper.getCompanyContactPerson());
		car.setCompanyContactPhone(viewhelper.getCompanyContactPhone());
		car.setCountryStyle(Integer.parseInt( viewhelper.getCountryStyle()));
		if ("".equals(viewhelper.getDisplacement())) {
			car.setDisplacement(0);
		} else {
			car.setDisplacement(Integer.parseInt(viewhelper.getDisplacement()));
		}

		car.setEngine(viewhelper.getEngine());
		car.setFuelType(Integer.parseInt(viewhelper.getFuelType()));
		car.setGearbox(Integer.parseInt(viewhelper.getGearbox()));
		car.setJqx(viewhelper.getJqx());
		if ("".equals(viewhelper.getLoadquality())) {
			car.setLoadquality(0);
		} else {
			car.setLoadquality(Integer.parseInt(viewhelper.getLoadquality()));
		}
		
		car.setBrandId(viewhelper.getBrandId());
		car.setVendorId(viewhelper.getVendorId());		
		car.setCarStyleId(viewhelper.getCarStyleId());
		
		car.setMemberId(Integer.parseInt(viewhelper.getMemberId()));
		if ("".equals(viewhelper.getMileage())) {
			car.setMileage(0);
		} else {
			car.setMileage(Integer.parseInt(StringUtil.removeDH(viewhelper.getMileage())));
		}

		car.setModelCode(viewhelper.getModelCode());
		car.setOwnerAddress(viewhelper.getOwnerAddress());
		car.setOwnerCardNo(viewhelper.getOwnerCardNo());
		car.setOwnerOrgNo(viewhelper.getOwnerOrgNo());
		car.setOwnerPhone(viewhelper.getOwnerPhone());
		car.setOwnerType(Integer.parseInt(viewhelper.getOwnerType()));
		car.setPassenger(Integer.parseInt(viewhelper.getPassenger()));
		if ("".equals(viewhelper.getPrice())) {
			car.setPrice(0);
		} else {
			car.setPrice(Integer.parseInt(StringUtil.removeDH(viewhelper.getPrice())));
		}

		car.setRegDate(viewhelper.getRegDate());
		car.setRegisterCardNo(viewhelper.getRegisterCardNo());
		car.setSendCardDate(viewhelper.getSendCardDate());
		car.setSerial(viewhelper.getSerial());
		
		car.setVendorCode(viewhelper.getVendorCode());
		car.setVin(viewhelper.getVin());
		car.setYearCarefulPeriod(viewhelper.getYearCarefulPeriod());
		car.setPhoto_1(viewhelper.getImg1());
		car.setPhoto_2(viewhelper.getImg2());
		car.setPhoto_3(viewhelper.getImg3());
		car.setPhoto_4(viewhelper.getImg4());
		car.setPhoto_5(viewhelper.getImg5());
		car.setPhoto_6(viewhelper.getImg6());
		car.setPhoto_7(viewhelper.getImg7());
		car.setPhoto_8(viewhelper.getImg8());
		car.setCarScore(viewhelper.getCarScore());
		car.setRegisterStatus(1);//入库
		car.setEntryStatus(1);//待录入
		car.setStatus(1);//正常
		car.setAttestStatus(viewhelper.getAttestStatus());//认证状态
		car.setCompanyContactPerson(viewhelper.getCompanyContactPerson());
		car.setCompanyContactPhone(viewhelper.getCompanyContactPhone());
		
		return car;
	}

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}
	
}
